package com.cwt_taxi_xa.util.baidu;

import java.io.File;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.*;

import android.graphics.drawable.BitmapDrawable;

import com.cwt_taxi_xa.ActivityCamera;
import com.cwt_taxi_xa.MainApplication;
import com.cwt_taxi_xa.R;
import com.cwt_taxi_xa.constants.Constants;
import com.cwt_taxi_xa.constants.SPConstants;

/**
 * 人脸对比
 */
public class FaceMatch {

    /**
     * 重要提示代码中所需工具类
     * FileUtil,Base64Util,HttpUtil,GsonUtils请从
     * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
     * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
     * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
     * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
     * 下载
     */
    public static String match() {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/face/v3/match";
        try {

            byte[] bytes1 = FileUtil.readFileByBytes("/mnt/sdcard/司机照片.jpg");
            byte[] bytes2 = FileUtil.readFileByBytes("/mnt/sdcard/test2.jpg");
            String image1 = Base64Util.encode(bytes1);
            String image2 = Base64Util.encode(bytes2);

            List<Map<String, Object>> images = new ArrayList<>();

            Map<String, Object> map1 = new HashMap<>();
            map1.put("image", image1);
            map1.put("image_type", "BASE64");
            map1.put("face_type", "LIVE");
            map1.put("quality_control", "LOW");
            map1.put("liveness_control", "NORMAL");

            Map<String, Object> map2 = new HashMap<>();
            map2.put("image", image2);
            map2.put("image_type", "BASE64");
            map2.put("face_type", "LIVE");
            map2.put("quality_control", "LOW");
            map2.put("liveness_control", "NORMAL");

            images.add(map1);
            images.add(map2);

            String param = GsonUtils.toJson(images);

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = AuthService.getAuth();

            String result = HttpUtil.post(url, accessToken, "application/json", param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 人脸比对
     * @param data
     * @return
     */
    public static String match(byte[] data1, byte[] data2) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/face/v3/match";
        try {
//        	ByteBuffer.allocate(ActivityCamera.getLoacalBitmap(Constants.sijiinfo.getPhoto()).getByteCount()).array()
//            byte[] bytes1 = FileUtil.readFileByBytes("/mnt/sdcard/司机照片.jpg");
//            byte[] bytes1 = ByteBuffer.allocate(((BitmapDrawable)MainApplication.topActivity.getResources().getDrawable(R.drawable.ligang)).getBitmap().getByteCount()).array();
//            byte[] bytes2 = data;
            String image1 = Base64Util.encode(data1);
            String image2 = Base64Util.encode(data2);


            File f;
            FileOutputStream fos;
            try {
                f = new File("/mnt/sdcard/APP-PIC_test1.jpg");
                if(f.exists()) f.delete();
                fos = new FileOutputStream(f);
                fos.write(data1);
                fos.flush();
                fos.close();
                f = new File("/mnt/sdcard/APP-PIC_test2.jpg");
                if(f.exists()) f.delete();
                fos = new FileOutputStream(f);
                fos.write(data2);
                fos.flush();
                fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }


            List<Map<String, Object>> images = new ArrayList<>();

            Map<String, Object> map1 = new HashMap<>();
            map1.put("image", image1);
            map1.put("image_type", "BASE64");
            map1.put("face_type", "LIVE");
            map1.put("quality_control", "LOW");
//            map1.put("liveness_control", "NORMAL");
//            map1.put("quality_control", "NONE");
            map1.put("liveness_control", "NONE");

            Map<String, Object> map2 = new HashMap<>();
            map2.put("image", image2);
            map2.put("image_type", "BASE64");
            map2.put("face_type", "LIVE");
            map2.put("quality_control", "LOW");
//            map2.put("liveness_control", "NORMAL");
//            map2.put("quality_control", "NONE");
            map2.put("liveness_control", "NONE");

            images.add(map1);
            images.add(map2);

            String param = GsonUtils.toJson(images);

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = AuthService.getAuth();

            String result = HttpUtil.post(url, accessToken, "application/json", param);
            System.out.println("--------------"+result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 数人头
     * @param data
     * @return
     */
    public static String detect(byte[] data) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/face/v3/detect";
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("image", Base64Util.encode(data));
            map.put("face_field", "faceshape,facetype");
            map.put("max_face_num", 10);
            map.put("image_type", "BASE64");

            String param = GsonUtils.toJson(map);

            String accessToken = AuthService.getAuth();

            String result = HttpUtil.post(url, accessToken, "application/json", param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static void main(String[] args) {
        FaceMatch.match();
    }
}